package Zadanie5;
import java.util.ArrayList;
import java.util.Random;


public class Zadanie  {

	public Zadanie(ArrayList<Integer> list , Window parent) {
		super();
		this.list = list;
		this.table = new int[256];
		this.parent = parent;
	}
	private Window parent;
	private ArrayList<Integer> list;
	private int[] table;
	private int threadNumber;
	private boolean semafor = false;
	private long startTime;
	private long countingTime;
	private ArrayList<MyThread> myThreads;
	private int cash = 0 ;
	private int prevGroup = 0 ;
	

	 public long getCountingTime() {
		return countingTime;
	}
	
	public ArrayList<MyThread> getMyThreads() {
		return myThreads;
	}

	public void setMyThreads(ArrayList<MyThread> myThreads) {
		this.myThreads = myThreads;
	}

	public void setCountingTime(long countingTime) {
		this.countingTime = countingTime;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public int[] getTable() {
		return table;
	}

	public void setTable(int[] table) {
		this.table = table;
	}

	public boolean isSemafor() {
		return semafor;
	}

	public synchronized boolean setSemafor() {
		if(semafor == false ){
			this.semafor = true;
			return true;
		}else
			return false;
	}

	public int getThreadNumber() {
		return threadNumber;
	}

	public void setThreadNumber(int threadNumber) {
		this.threadNumber = threadNumber;
	}
	public synchronized void decrementhreadNumber() {
		--this.threadNumber;
		if(threadNumber == 0 ){
			setCountingTime(System.currentTimeMillis() - getStartTime());
			if(parent instanceof onCountingComplete)
				parent.CountingComplete();
		}
	}

	public synchronized void addCash(int idWatku, int group ){
		long startTime = System.currentTimeMillis();
		Random random = new Random();
/*		while(group == prevGroup){
			try {
				wait(random.nextInt(10));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		if(group == prevGroup)
			try {
				wait(random.nextInt(10));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		parent.appendPrint(" \nDziala watek "+ idWatku + " grupa " + group);
		if(group != 1 ){
		for(int i = 0 ; i < 10; i++)
			++cash; 
		 }else {
			 cash -= 5;
		 }		
		prevGroup = group;
		parent.appendPrint(" Kwota : "+cash + " czas "+ (System.currentTimeMillis() - startTime)+ " ms \n");
		notifyAll();
	 }
	

	public void start(int thread_number_group1,int thread_number_group2 ) {
		clearTable();
//		setThreadNumber(thread_number);
		
			myThreads = new ArrayList<Zadanie.MyThread>();
			setStartTime(System.currentTimeMillis());
			for(int i = 0 ; i < thread_number_group1;i++)
				myThreads.add(new MyThread(i,1) );
			for(int i = 0 ; i < thread_number_group2;i++)
				myThreads.add(new MyThread(i,2) );
  }
	
	class MyThread extends Thread {
		
		int id;
		int group;
		long threadCountingTime;
		long threadStartTime;
				
		public MyThread(int id,int group) {
			super();
			this.id = id;
			this.group = group;
			start();
		}

		@Override
		public void run() {
			//threadStartTime = System.currentTimeMillis();
			addCash(id,group);
		/*	if(group != 1 ){
			for(int i = 0 ; i < 10; i++)
				++cash; 
			 }else {
				 cash -= 5;
			 }		
			parent.appendPrint("\nWatek "+ id + " grupa " + group +" Kwota : "+cash + " czas "+ (System.currentTimeMillis() - threadStartTime)+ " ms \n");
			//threadCountingTime = System.currentTimeMillis() - threadStartTime;
			//parent.appendPrint("\n czas " + threadCountingTime +"ms dla id "+ id + " grupa " +group + "\n");
			*/
		}	
		public long getThreadCountingTime(){
			return threadCountingTime;
		}

	}
	
	public void clearTable(){
		for(int i = 0 ; i < 256; i++){
			table[i]=0;
		}
	}

	
}
	
